--[[-----------------------------------------------------]]--
--	Creates getter and setter accessors for client
--	and server access assuming they are DT vars
--[[-----------------------------------------------------]]--
function AccessorFuncDT( tbl, varname, name )
   tbl[ "Get" .. name ] = function( s ) return s.dt[ varname ] end
   tbl[ "Set" .. name ] = function( s, v ) s.dt[ varname ] = v end
end

function AccessorFuncIN( tbl, enum, name )
	tbl[ "Is" .. name ] = function( s ) return s:KeyDown( enum ) end
end

--[[-----------------------------------------------------]]--
--	Returns the given value if it is a valid object
--	of the given type or the default value
--[[-----------------------------------------------------]]--
function ValOrDef( val, tp, def )

	--	Classes
	if IsValidInstance( val, tp ) then
		return val
	end

	-- If type is not a string flatten it
	if not type( tp ) == "string" then
		tp		= tostring( tp )
	end

	--	Checks if the value is of the given type
	if type( val ) == tp then
		return val
		
	--	Special case: Color
	elseif tp == "Color" then
		if IsColor( val ) then return val
		else return def end
		
	--	Special case: Angle
	elseif tp == "Vector" then
		if isvector( val ) then return val
		else return def end
		
	--	Special case: integer
	elseif tp == "integer" then
		if IsInt( val ) then return val
		else return def end
		
	--	Special case: float
	elseif tp == "float" then
		if IsFloat( val ) then return val
		else return def end
		
	--	Otherwise return default value
	elseif type( def ) == tp then
		return def
		
	--	Default value for classes
	elseif IsValidInstance( def, tp ) then
		return def
		
	--	Invalid default value
	else
		DEBUG:ErrMsg( "The default value '" .. tostring( def ) .. "' is not of the type '" .. tostring( tp ) .. "'", "ValOrDef" )
		return nil
	end

end

--[[-----------------------------------------------------]]--
--	Converts a bool into an int
--[[-----------------------------------------------------]]--
function BoolToInt( bool )

	if IsString( bool ) then
		bool = tobool( bool )
	end

	if IsBool( bool ) then
		if bool then return 1
		else return 0 end
	
	elseif IsInt( bool ) then
		if bool > 1 then return 1
		elseif bool < 0 then return 0
		else return bool end
	end
	
	return 0

end

--[[-----------------------------------------------------]]--
--	Converts an int into a bool
--[[-----------------------------------------------------]]--
function IntToBool( int )

	if IsString( int ) then
		int = tonumber( int )
	end
	
	if IsInt( int ) then
		if int > 1 then return true
		else return false end
		
	elseif IsBool( int ) then
		return int
	end
	
	return false

end